package com.gitee.xhs.dao;

import cn.hutool.core.date.DateUtil;
import com.gitee.xhs.sampleRecod.transactionRecord.Transaction;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author BigHead
 * @date 2022/7/25 11:04
 * @description AggregateTransactionDao
 */
public class AggregateTransactionDao {

    private Long accountId;
    private Long minTimeStamp;
    private Long maxTimeStamp;
    private final List<SimpleTransactionDao> simpleTransactionDaoList = new ArrayList<>();

    public void setTransaction(Transaction transaction) {
        if (null == accountId) {
            accountId = transaction.getAccountId();
        }
        if (null == minTimeStamp) {
            minTimeStamp = transaction.getTimestamp();
        } else {
            if (minTimeStamp > transaction.getTimestamp()) {
                minTimeStamp = transaction.getTimestamp();
            }
        }

        if (null == maxTimeStamp) {
            maxTimeStamp = transaction.getTimestamp();
        } else {
            if (maxTimeStamp < transaction.getTimestamp()) {
                maxTimeStamp = transaction.getTimestamp();
            }
        }

        SimpleTransactionDao simpleTransactionDao = new SimpleTransactionDao(
                transaction.getDataNum(),
                transaction.getTimestamp()
        );
        simpleTransactionDaoList.add(simpleTransactionDao);
    }

    public AggregateTransactionDao merge(AggregateTransactionDao aggregateTransactionDao) {
        if (this.accountId.equals(aggregateTransactionDao.accountId)) {
            if (minTimeStamp > aggregateTransactionDao.minTimeStamp) {
                minTimeStamp = aggregateTransactionDao.minTimeStamp;
            }
            if (maxTimeStamp < aggregateTransactionDao.maxTimeStamp) {
                maxTimeStamp = aggregateTransactionDao.maxTimeStamp;
            }
            simpleTransactionDaoList
                    .addAll(
                            aggregateTransactionDao.simpleTransactionDaoList
                    );
        }
        return this;
    }

    @Override
    public String toString() {
        List<String> simpleTransactionShowList =
                simpleTransactionDaoList
                        .stream()
                        .sorted(
                                Comparator.comparingInt(SimpleTransactionDao::getDataNum)
                        )
                        .map(
                                Object::toString
                        )
                        .collect(Collectors.toList());

        return "AggregateTransactionDao{" +
                "accountId=" + accountId +
                ", count=" + simpleTransactionDaoList.size() +
                ", minTimeStamp=" + minTimeStamp +
                ", minTimeStampString=" + DateUtil.formatDateTime(new Date(minTimeStamp)) +
                ", maxTimeStamp=" + maxTimeStamp +
                ", maxTimeStampString=" + DateUtil.formatDateTime(new Date(maxTimeStamp)) +
                ", simpleTransactionDaoList=" + String.join(",", simpleTransactionShowList) +
                '}';
    }

    static class SimpleTransactionDao {
        private final Integer dataNum;
        private final Long dataTimestamp;

        SimpleTransactionDao(Integer dataNum, Long dataTimestamp) {
            this.dataNum = dataNum;
            this.dataTimestamp = dataTimestamp;
        }

        public Integer getDataNum() {
            return dataNum;
        }

        @Override
        public String toString() {
            return "SimpleTransactionDao{" +
                    "dataNum=" + dataNum +
                    ", dataTimestamp=" + dataTimestamp +
                    ", dataTimestampString=" + DateUtil.formatDateTime(new Date(dataTimestamp)) +
                    '}';
        }
    }

}
